﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace HPKaraoke
{
    // Lớp playlist - chứa danh sách những bài hát được phát
    public class LibraryController
    {
        /// <summary>
        /// List các bài hát
        /// </summary>
        protected static List<SongGUI> SongList = new List<SongGUI>();

        public static SongGUI FindByID(int id)
        {
            for (int i = 0; i < SongList.Count; ++i)
                if (id == SongList[i].song.Information.ID)
                {
                    return SongList[i];
                }
            return null;
        }

        public static int LoadAllSongInLibrary(string Folder)
        {
            string[] filePaths = Directory.GetFiles(Folder, "*.inf");

            SongList = null;
            SongList = new List<SongGUI>();
            if (filePaths.Count() > 0)
            {
                foreach (var fileDir in filePaths)
                {
                    SongGUI s = new XmlSongGUI(ConfigurationSettings.AppSettings["ZingDataPath"], fileDir);
                    SongList.Add(s);
                }
            }
            return filePaths.Count();

        }

        public static List<SongInfo> Find(string keyWord)
        {
            keyWord = keyWord.ToLower();
            List<SongInfo> result = new List<SongInfo>();
            for (int i = 0; i < SongList.Count; ++i)
            {
                if ((SongList[i].song.Information.Author.ToLower().Contains(keyWord))
                    || (SongList[i].song.Information.Singer.ToLower().Contains(keyWord))
                    || (SongList[i].song.Information.Title.ToLower().Contains(keyWord))
                    )
                {
                    result.Add(SongList[i].song.Information);
                }
            }
            return result;
        }

        public static void Dispose()
        {
            for (int i = 0; i < SongList.Count; ++i)
            {
                SongList[i].Dispose();
            }
        }

    }
}
